package backend.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import backend.Util;

public class ConnectionManager {
  private String url = null;
  private String username = null;
  private String password = null;
  private Connection conn = null;
  
  public ConnectionManager(final String _url, final String _username, final String _password) {
    url = _url;
    username = _username;
    password = _password;
  }
  
  private Connection newConnection() throws SQLException {
    return DriverManager.getConnection(url, username, password);
  }
  
  public Statement createStatement() throws SQLException {
    return conn.createStatement();
  }
  
  public void connect() throws SQLException {
    conn = newConnection();
    conn.setAutoCommit(false);
    conn.setTransactionIsolation(4);
  }
  
  public Connection getConn() {
    return conn;
  }
  
  public void disconnect() {
    Util.tryClose(conn);
  }
}
